import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store"

Vue.use(VueRouter)

//2.给每个路由设置单独的权限(路由独享守卫)
function checkRoute(url, next) {
    //查看登陆后的id中是否包含想去的路径包含的话去 否则去首页
    if (store.state.info.menus_url.includes(url)) {
        next()
    } else {
        next("/index")
    }
}
export let IndexRoutes = [{
    path: "manage",
    component: () =>
        import ("../pages/manage/manage.vue"),
    meta: {
        title: "管理员管理"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/manage", next)
    }
}, {
    path: "menu",
    component: () =>
        import ("../pages/menu/menu.vue"),
    meta: {
        title: "菜单管理"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/menu", next)
    }
}, {
    path: "role",
    component: () =>
        import ("../pages/role/role.vue"),
    meta: {
        title: "角色管理"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/role", next)
    }
}, {
    path: "vip",
    component: () =>
        import ("../pages/vip/vip.vue"),
    meta: {
        title: "会员管理"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/vip", next)
    }
}, {
    path: "banner",
    component: () =>
        import ("../pages/banner/banner.vue"),
    meta: {
        title: "轮播图管理"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/banner", next)
    }
}, {
    path: "specs",
    component: () =>
        import ("../pages/specs/specs.vue"),
    meta: {
        title: "商品规格"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/specs", next)
    }
}, {
    path: "seckill",
    component: () =>
        import ("../pages/seckill/seckill.vue"),
    meta: {
        title: "秒杀活动"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/seckill", next)
    }
}, {
    path: "cate",
    component: () =>
        import ("../pages/cate/cate.vue"),
    meta: {
        title: "商品分类"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/cate", next)
    }
}, {
    path: "goods",
    component: () =>
        import ("../pages/goods/goods.vue"),
    meta: {
        title: "商品管理"
    },
    beforeEnter(to, from, next) {
        checkRoute("/index/goods", next)
    }
}, ]

const routes = [{
        path: "/login",
        component: () =>
            import ("../pages/login/login.vue")
    },
    {
        path: "/index",
        component: () =>
            import ("../pages/index/index.vue"),
        children: [{
                path: "",
                component: () =>
                    import ("../pages/home/home.vue")
            },
            ...IndexRoutes,
            {
                path: "*",
                redirect: "home"
            },
        ]

    },
    {
        path: "*",
        redirect: "/login"
    },

]

const router = new VueRouter({
        routes
    })
    //全局守卫 登录拦截
router.beforeEach((to, from, next) => {
    //登录可以进入
    if (to.path == "/login") {
        next()
        return
    }
    //2.前往的不是的登录 验证vuex 本地的数据 
    if (!!store.state.info.token) {
        next()
        return
    }
    // 3.vuex|本地存储没有数据，next('/login')
    next("/login")

})
export default router